package com.om.base;

import java.sql.Connection;
import java.util.Date;


public abstract class BaseConnectionAdapter {
	
	protected int openerThreadId=0;
	protected Date openTime=null;
		 
    protected DBConnectionManager dbConnectionManager = null;
	
    abstract public Connection getNewConnection(String name);
	
	abstract public Connection getAvailableConnection();
	
	abstract public Connection getAvailableConnection(boolean forceOpenNew);
	
	abstract public Connection getWriteConnection();
	
	public int getOpenerThreadId() {
		return openerThreadId;
	}

	public void setOpenerThreadId(int openerThreadId) {
		this.openerThreadId = openerThreadId;
	}

	public Date getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	

	public DBConnectionManager getDbConnectionManager() {
		return dbConnectionManager;
	}

	public void setDbConnectionManager(DBConnectionManager dbConnectionManager) {
		this.dbConnectionManager = dbConnectionManager;
	}

	public Connection getAvailableConnection(String name) {
		Connection con =null;
		try{
			con = this.dbConnectionManager.getConnection(name);
			if(con==null){
				con =this.getNewConnection(name);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		// TODO Auto-generated method stub
		return con;
	}
	
	
	
	public Connection getAvailableConnection(String name,boolean forceOpenNew) {
		Connection con =null;
		try{
			con = this.dbConnectionManager.getConnection(name,forceOpenNew);
			if(con==null && forceOpenNew){
				con =this.getNewConnection(name);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		// TODO Auto-generated method stub
		return con;
	}
	
	
	
	
}
